package dws_fld

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"server/utils"
)

type DwsFldApi struct {
}

var dwsFldService = DwsFldService{}


func (dwsFldApi *DwsFldApi) CreateDwsFld(c *gin.Context) {
	var dwsFld DwsFld
	err := c.ShouldBindJSON(&dwsFld)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsFld.CreatedBy = utils.GetUserID(c)
	verify := utils.Rules{
		"ApiId":      {utils.NotEmpty()},
		"DwsTbId":    {utils.NotEmpty()},
		"SrcFldName": {utils.NotEmpty()},
		"SrcFldType": {utils.NotEmpty()},
		"DwsFldName": {utils.NotEmpty()},
	}
	if err := utils.Verify(dwsFld, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := dwsFldService.CreateDwsFld(&dwsFld); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}


func (dwsFldApi *DwsFldApi) DeleteDwsFld(c *gin.Context) {
	var dwsFld DwsFld
	err := c.ShouldBindJSON(&dwsFld)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsFld.DeletedBy = utils.GetUserID(c)
	if err := dwsFldService.DeleteDwsFld(dwsFld); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}


func (dwsFldApi *DwsFldApi) DeleteDwsFldByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	deletedBy := utils.GetUserID(c)
	if err := dwsFldService.DeleteDwsFldByIds(IDS, deletedBy); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}


func (dwsFldApi *DwsFldApi) UpdateDwsFld(c *gin.Context) {
	var dwsFld DwsFld
	err := c.ShouldBindJSON(&dwsFld)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsFld.UpdatedBy = utils.GetUserID(c)
	verify := utils.Rules{
		"ApiId":      {utils.NotEmpty()},
		"DwsTbId":    {utils.NotEmpty()},
		"SrcFldName": {utils.NotEmpty()},
		"SrcFldType": {utils.NotEmpty()},
		"DwsFldName": {utils.NotEmpty()},
	}
	if err := utils.Verify(dwsFld, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := dwsFldService.UpdateDwsFld(dwsFld); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}


func (dwsFldApi *DwsFldApi) FindDwsFld(c *gin.Context) {
	var dwsFld DwsFld
	err := c.ShouldBindQuery(&dwsFld)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if redwsFld, err := dwsFldService.GetDwsFldById(dwsFld.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"redwsFld": redwsFld}, c)
	}
}


func (dwsFldApi *DwsFldApi) GetDwsFldList(c *gin.Context) {
	var pageInfo DwsFldSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := dwsFldService.GetDwsFldInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}


func (dwsFldApi *DwsFldApi) GetAllDwsFlds(c *gin.Context) {
	dwsFld, err := dwsFldService.GetAllDwsFlds()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"dwsFld": dwsFld}, "获取成功", c)
	}
}


func (dwsFldApi *DwsFldApi) GetDwsFld(c *gin.Context) {
	dwsFld, err := dwsFldService.GetDwsFld()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"dwsFld": dwsFld}, "获取成功", c)
	}
}


func (dwsFldApi *DwsFldApi) InitDwsFld(c *gin.Context) {
	response.FailWithMessage("获取初始化数据失败", c)
}
